package com.tarena.lbs.basic.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.basic.dao.mapper.AreaMapper;
import com.tarena.lbs.basic.dao.repository.AreaRepository;
import com.tarena.lbs.basic.pojo.po.AreaPO;
import com.tarena.lbs.basic.pojo.vo.AreaVO;
import com.tarena.lbs.basic.service.AreaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class AreaServiceImpl implements AreaService {
    private static final Logger log = LoggerFactory.getLogger(AreaServiceImpl.class);
    @Autowired
    private AreaRepository areaRepository;

    @Override
    public List<AreaVO> getChildren(Integer parentId) {
       // 初始化一个空的AreaVO列表，用于存放转换后的对象
        List<AreaVO> vos = new ArrayList<>();
        // 调用areaRepository的getChildren方法获取指定parentId下的所有子区域
        List<AreaPO> pos = areaRepository.getChildren(parentId);
        // 检查获取到的子区域列表是否非空，以决定是否进行转换操作
        if (CollectionUtils.isNotEmpty(pos)) {
            // 使用Java 8的Stream API对子区域列表进行转换，每个AreaPO对象都被转换为一个AreaVO对象
            vos = pos.stream().map(po -> {
                // 创建一个新的AreaVO对象
                AreaVO vo = new AreaVO();
                // 使用Spring的BeanUtils工具类，将AreaPO对象的属性值复制到新创建的AreaVO对象中
                BeanUtils.copyProperties(po, vo);
                // 返回转换后的AreaVO对象
                return vo;
            }).collect(Collectors.toList());
}

        return vos;
    }

    @Override
    public List<AreaVO> getTree(Object o) {
        List<AreaPO> pos=areaRepository.getAll();
        List<AreaVO> parentVOS = pos.stream()
                .filter(po -> po.getParentId() == 0)
                .map(po -> {
                    AreaVO vo = new AreaVO();
                    BeanUtils.copyProperties(po, vo);
                    List<AreaVO> childVOS=pos.stream().filter(p -> p.getParentId().equals(po.getId()))
                            .map(p -> {
                                AreaVO child = new AreaVO();
                                BeanUtils.copyProperties(p, child);
                                List<AreaVO> childrenVOS =pos.stream().filter(p1 -> p1.getParentId().equals(p.getId()))
                                        .map(p1 -> {
                                            AreaVO children = new AreaVO();
                                            BeanUtils.copyProperties(p1, children);
                                            return children;
                                        }).collect(Collectors.toList());
                                child.setChildren(childrenVOS);
                                return child;
                            }).collect(Collectors.toList());
                    vo.setChildren(childVOS);
                    return vo;
                })
                .collect(Collectors.toList());
        return parentVOS;
    }

    @Override
    public List<String> queryArea(List<Long> idList) {
        log.info("查询参数列表{}",idList);
        List<AreaPO> areaList = areaRepository.queryArea(idList);
        if (areaList != null && areaList.size() > 0) {
            List<String> areaNameList = areaList.stream().map(AreaPO::getName).collect(Collectors.toList());
            return areaNameList;
        }
        return Collections.emptyList();
    }
}